#!/bin/sh
# $Id: $
# Distribute hook script, run by make automatically

# get fileinfo & version
fileinfo=
filetype=BETA
devel=`cat config.h | grep PACKAGE_VERSION | cut -d \" -f 2 | grep "dev$"`
if test -z "$devel" ; then
  fileinfo=`cat VERSION`
  git tag $fileinfo -a -m "Release: $fileinfo" 
  if test $? -ne 0 ; then
    exit -1
  fi
  fileinfo2=`echo $fileinfo | sed 's/[a-zA-Z]//g'`
  if test "$fileinfo" = "$fileinfo2" ; then
    filetype=RELEASE
  fi
else
  fileinfo=dev
fi

# make tar ball
echo "Creating package file: xunsearch-full-$fileinfo ..."
ln -s xunsearch-full xunsearch-full-$fileinfo
tar -cHjf xunsearch-full-$fileinfo.tar.bz2 xunsearch-full-$fileinfo
rm -f xunsearch-full-$fileinfo

# upload to web server
echo "Uploading file to web server ($filetype) ..."
if test "$fileinfo" = "dev" ; then
  scp xunsearch-full-$fileinfo.tar.bz2 xunsearch:./web/download
  scp xunsearch-full/packages/xunsearch-sdk-*-dev.zip xunsearch:./web/download/xunsearch-sdk-dev.zip
  scp VERSION xunsearch:./web/protected/data
else
  scp xunsearch-full-$fileinfo.tar.bz2 xunsearch:./web/download/xunsearch-full
  if test "$filetype" = "RELEASE" ; then
    ssh xunsearch ln -sf xunsearch-full/xunsearch-full-$fileinfo.tar.bz2 ./web/download/xunsearch-full-latest.tar.bz2 
	scp xunsearch-full/packages/xunsearch-sdk-$fileinfo.zip xunsearch:./web/download/xunsearch-sdk-latest.zip
  fi
  scp VERSION xunsearch:./web/protected/data/$filetype
  # also update dev
  scp xunsearch-full-$fileinfo.tar.bz2 xunsearch:./web/download/xunsearch-full-dev.tar.bz2
  scp xunsearch-full/packages/xunsearch-sdk-$fileinfo.zip xunsearch:./web/download/xunsearch-sdk-dev.zip
  scp VERSION xunsearch:./web/protected/data
fi

# clean local file
rm -f xunsearch-full-$fileinfo.tar.bz2

exit 0
